package com.ruoyi.team.collegeContact.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.team.collegeContact.domain.CollegeContact;
import com.ruoyi.team.collegeContact.service.ICollegeContactService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学院通讯录，用于存储学院成员的联系信息Controller
 *
 * @author ruoyi
 * @date 2025-01-25
 */
@RestController
@RequestMapping("/team/collegeContact")
public class CollegeContactController extends BaseController
{
    @Autowired
    private ICollegeContactService collegeContactService;

    /**
     * 查询学院通讯录，用于存储学院成员的联系信息列表
     */
    @PreAuthorize("@ss.hasPermi('team:collegeContact:list')")
    @GetMapping("/list")
    public TableDataInfo list(CollegeContact collegeContact)
    {
        startPage();
        List<CollegeContact> list = collegeContactService.selectCollegeContactList(collegeContact);
        return getDataTable(list);
    }

    /**
     * 导出学院通讯录，用于存储学院成员的联系信息列表
     */
    @PreAuthorize("@ss.hasPermi('team:collegeContact:export')")
    @Log(title = "学院通讯录，用于存储学院成员的联系信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CollegeContact collegeContact)
    {
        List<CollegeContact> list = collegeContactService.selectCollegeContactList(collegeContact);
        ExcelUtil<CollegeContact> util = new ExcelUtil<CollegeContact>(CollegeContact.class);
        util.exportExcel(response, list, "学院通讯录，用于存储学院成员的联系信息数据");
    }

    /**
     * 获取学院通讯录，用于存储学院成员的联系信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('team:collegeContact:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(collegeContactService.selectCollegeContactById(id));
    }

    /**
     * 新增学院通讯录，用于存储学院成员的联系信息
     */
    @PreAuthorize("@ss.hasPermi('team:collegeContact:add')")
    @Log(title = "学院通讯录，用于存储学院成员的联系信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CollegeContact collegeContact)
    {
        return toAjax(collegeContactService.insertCollegeContact(collegeContact));
    }

    /**
     * 修改学院通讯录，用于存储学院成员的联系信息
     */
    @PreAuthorize("@ss.hasPermi('team:collegeContact:edit')")
    @Log(title = "学院通讯录，用于存储学院成员的联系信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CollegeContact collegeContact)
    {
        return toAjax(collegeContactService.updateCollegeContact(collegeContact));
    }

    /**
     * 删除学院通讯录，用于存储学院成员的联系信息
     */
    @PreAuthorize("@ss.hasPermi('team:collegeContact:remove')")
    @Log(title = "学院通讯录，用于存储学院成员的联系信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(collegeContactService.deleteCollegeContactByIds(ids));
    }

    /**
     * 查询是否展示学院通讯录条目
     */
    @PreAuthorize("@ss.hasPermi('team:collegeContact:query')")
    @GetMapping("/visible")
    public AjaxResult isVisible()
    {
        boolean isVisible = collegeContactService.isCollegeContactVisible();
        return AjaxResult.success(isVisible);
    }


    /**
     * 更新所有学院通讯录条目的 is_displayed 字段
     */
    @PreAuthorize("@ss.hasPermi('team:collegeContact:updateAll')")
    @Log(title = "学院通讯录，用于存储学院成员的联系信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateAllIsDisplayed/{isDisplayed}")
    public AjaxResult updateAllIsDisplayed(@PathVariable Integer isDisplayed)
    {
        if (isDisplayed == null || (isDisplayed != 0 && isDisplayed != 1)) {
            return AjaxResult.error("isDisplayed 参数必须为 0 或 1");
        }
        int result = collegeContactService.updateAllCollegeContactIsDisplayed(isDisplayed);
        return toAjax(result);
    }


    /**
     * 删除所有学院通讯录条目，并从 sys_user 表中获取数据插入到 CollegeContact 表中，is_displayed 设置为 0
     */
    @PreAuthorize("@ss.hasPermi('team:collegeContact:reset')")
    @Log(title = "学院通讯录，用于存储学院成员的联系信息", businessType = BusinessType.UPDATE)
    @PutMapping("/resetFromSysUser")
    public AjaxResult resetFromSysUser()
    {
        collegeContactService.resetCollegeContactFromSysUser();
        return AjaxResult.success("重置成功");
    }

}
